# encoding:utf-8
class LinkedListQueue:
    """
    基于链表实现的队列
    """
    def __init__(self):
        self.__front: ListNode = None    # 头节点 front
        self.__rear: ListNode = None    # 尾节点 rear
        self.__size: int = 0    # 队列的长度

    def size(self) -> int:
        """获取队列的长度"""
        return self.__size

    def is_empty(self) -> bool:
        """判断队列是否为空"""
        return not self.__front

    def push(self, num: int) -> None:
        """入队"""
        # 尾节点后添加 num
        node = ListNode(num)
        # 如果队列为空，则令头、尾节点都指向 node
        if self.__front is None:
            self.__front = node
            self.__rear = node
        # 如果队列不为空，则将 node 添加到尾节点 rear 后
        else:
            self.__rear.next = node
            self.__rear = node
        self.__size += 1

    def pop(self) -> int:
        """出队"""
        num = self.peek()
        # 删除头节点
        self.__front = self.__front.next
        self.__size -= 1
        return num

    def peek(self) -> int:
        """访问队首元素"""
        # 队列为空
        if self.__front is None:
            raise IndexError("队列为空")
        return self.__front.val

    def to_list(self) -> list[int]:
        """返回列表用于打印"""
        node = self.__front
        res = []
        while node:
            res.append(node.val)
            node = node.next
        return res



